package com.lecture.java8.excel.dyr.k;

import com.alibaba.excel.EasyExcel;
import com.lecture.java8.excel.DemoDataListener;
import com.lecture.java8.excel.dyr.j.Model0729;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Test0801 {

    public static void main(String[] args) {
        String dir = "D:\\study\\tmp\\0801\\";


        String resultPath = dir + "“低碳城市”三批试点城市名单DID.xlsx";
        DemoDataListener<Result02> dataResultListener = new DemoDataListener<>();
        EasyExcel.read(resultPath, Result02.class,dataResultListener).sheet().doRead();
        //全要素生产率
        List<Result02> resultList = dataResultListener.getDataList();
        Map<String,String> resultMap1 = new HashMap<>();
        for (Result02 result02 : resultList) {
            resultMap1.put(result02.getC()+"市",result02.getI());
        }

        String resultPath2 = dir + "宽带中国-地级市版(2000-2022年)(1).xlsx";
        DemoDataListener<Result01> data2 = new DemoDataListener<>();
        EasyExcel.read(resultPath2, Result01.class,data2).sheet().doRead();
        //全要素生产率
        List<Result01> resultList2 = data2.getDataList();
        Map<String,String> resultMap2 = new HashMap<>();
        for (Result01 result02 : resultList2) {
            resultMap2.put(result02.getC()+"市",result02.getI());
        }

        String resultPath3 = dir + "2013年带行业变量的.xlsx";
        DemoDataListener<Model0801> data3 = new DemoDataListener<>();
        EasyExcel.read(resultPath3, Model0801.class,data3).sheet().doRead();
        //全要素生产率
        List<Model0801> resultList3 = data3.getDataList();
        Set<String> code = new HashSet<>();
        for (Model0801 model0801 : resultList3) {
            if (StringUtils.isBlank(resultMap1.get(model0801.getW()))) {
               // System.out.println(model0801.getW() + "DID低碳为空");
                code.add(model0801.getW());
            }
            if (StringUtils.isBlank(resultMap2.get(model0801.getW()))) {
                //System.out.println(model0801.getW() + "DID");
                code.add(model0801.getW());
            }
            model0801.setAaai(resultMap1.get(model0801.getW()));
            model0801.setAiii(resultMap2.get(model0801.getW()));
        }
        for (String s : code) {
            System.out.println(s);
        }

        EasyExcel.write( dir + "0801-1结果.xlsx", Model0801.class)
                .sheet("a").doWrite(resultList3);

    }

    public static List<Model0729> filterCompleteYears(List<Model0729> list, int startYear, int endYear) {
        Set<Integer> requiredYears = IntStream.rangeClosed(startYear, endYear)
                .boxed()
                .collect(Collectors.toSet());

        // Group by code and filter
        Map<String, Set<Integer>> codeToYearsMap = list.stream()
                .collect(Collectors.groupingBy(
                        Model0729::getA,
                        Collectors.mapping(Model0729::getC, Collectors.toSet())
                ));


        Set<String> validCodes = codeToYearsMap.entrySet().stream()
                .filter(entry -> entry.getValue().equals(requiredYears))
                .map(Map.Entry::getKey)
                .collect(Collectors.toSet());

        // Filter original list by valid codes
        return list.stream()
                .filter(obj -> validCodes.contains(obj.getA()))
                .collect(Collectors.toList());
    }
}
